package com.example.electronicmall;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.electronicmall.fragment.HomeFragment;
import com.example.electronicmall.fragment.MyFragment;
import com.example.electronicmall.fragment.NewsFragment;
import com.example.electronicmall.fragment.RecommendFragment;
import com.example.electronicmall.fragment.SearchFragment;
import com.example.electronicmall.fragment.ShoppingCartFragment;
import com.example.electronicmall.fragment.SubscribeFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
    HomeFragment homeFragment;
    SearchFragment searchFragment;
    NewsFragment newsFragment;
    ShoppingCartFragment shoppingCartFragment;
    MyFragment myFragment;

    private String TAG=MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
    }

    public   void abc(View v){
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        RecommendFragment recommendFragment=new RecommendFragment();
        SubscribeFragment subscribeFragment=new SubscribeFragment();
        switch (v.getId()){
            case R.id.home_subscribe:
                ft.replace(R.id.frangment_home,subscribeFragment);
                break;
            case R.id.hone_recommend:
                ft.replace(R.id.frangment_home,recommendFragment);
                break;
        }
        ft.commit();

    }

    private void initview(){
        BottomNavigationView bottom=findViewById(R.id.home_navi);

        homeFragment=new HomeFragment();
        searchFragment=new SearchFragment();
        newsFragment =new NewsFragment();
        shoppingCartFragment=new ShoppingCartFragment();
        myFragment=new MyFragment();

        fragmentArrayList.add(homeFragment);
        fragmentArrayList.add(searchFragment);
        fragmentArrayList.add(newsFragment);
        fragmentArrayList.add(shoppingCartFragment);
        fragmentArrayList.add(myFragment);

        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();

        for (int i = 0; i <fragmentArrayList.size() ; i++) {
            ft.add(R.id.home_fragment_contaiar, fragmentArrayList.get(i));
            ft.hide(fragmentArrayList.get(i));
        }
            ft.show(fragmentArrayList.get(0));
        ft.commit();
        bottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull  MenuItem item) {
                showFragment(item.getItemId());
                return true;
            }
        });

    }
    private void showFragment(int items){
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();

        for (int i = 0; i < fragmentArrayList.size(); i++) {
            ft.hide(fragmentArrayList.get(i));
        }
        switch (items){
            case R.id.home_navi_item_home:
                ft.replace(R.id.home_fragment_contaiar,homeFragment);
                ft.show(homeFragment);
                break;
            case R.id.home_navi_item_search:
                ft.replace(R.id.home_fragment_contaiar,searchFragment);
                ft.show(searchFragment);
                break;
            case R.id.home_navi_item_news:
                ft.replace(R.id.home_fragment_contaiar,newsFragment);
                ft.show(newsFragment);
                break;
            case R.id.home_navi_item_shoppingcart:
                ft.replace(R.id.home_fragment_contaiar,shoppingCartFragment);
                ft.show(shoppingCartFragment);
                break;
            case R.id.home_navi_item_my:
                ft.replace(R.id.home_fragment_contaiar,myFragment);
                ft.show(myFragment);
                break;
        }
        ft.commit();
    }
}